/*
 * Copyright 2011 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.android.calllog.model;

import java.io.InputStream;
import java.util.Date;

import nl.biz_e.android.calllog.CallLogApp;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
/**
 * Represents an entry in the systems phone log.<br/>
 * Also offers a simple way to retrieve the contacts avatar. (See {@link #getThumb()})
 * 
 * @author Joost A. Bloemsma
 */
public class CallLogItem {
	private String cachedName;
	private String cachedNumberLabel;
	private int cachedNumberType;
	private Date date;
	private int duration;
	private String number;
	private int type;
	private int contactId;
	private String contactUri = null;
	private String contactLookupKey = null;
	private Bitmap thumb = null;
	
	/** Fixed string, used as a prefix for the LogItemId */
	public static String IDPREFIX = "LOGITEMID#";
		
	/**
	 * Constructs a new CallLogItem instance
	 */
	public CallLogItem() {
		super();
	}
	
	/**
	 * Constructs a new CallLogItem instance
	 * @param callerId
	 * @param number
	 * @param date
	 * @param duration
	 */
	public CallLogItem(String callerId, String number, Date date, int duration) {
		this.setCachedName(callerId);
		this.setNumber(number);
		this.setDate(date);
		this.setDuration(duration);
	}
	
	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}
	/**
	 * @param date the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}
	/**
	 * @return the number
	 */
	public String getNumber() {
		return number;
	}
	/**
	 * @param number the number to set
	 */
	public void setNumber(String number) {
		this.number = number;
	}

	/**
	 * @return the duration
	 */
	public int getDuration() {
		return duration;
	}

	/**
	 * @param duration the duration to set
	 */
	public void setDuration(int duration) {
		this.duration = duration;
	}

	/**
	 * @return the cachedName
	 */
	public String getCachedName() {
		return cachedName;
	}

	/**
	 * @param cachedName the cachedName to set
	 */
	public void setCachedName(String cachedName) {
		this.cachedName = cachedName;
	}

	/**
	 * @return the cachedNumberLabel
	 */
	public String getCachedNumberLabel() {
		return cachedNumberLabel;
	}

	/**
	 * @param cachedNumberLabel the cachedNumberLabel to set
	 */
	public void setCachedNumberLabel(String cachedNumberLabel) {
		this.cachedNumberLabel = cachedNumberLabel;
	}

	/**
	 * @return the cachedNumberType
	 */
	public int getCachedNumberType() {
		return cachedNumberType;
	}

	/**
	 * @param cachedNumberType the cachedNumberType to set
	 */
	public void setCachedNumberType(int cachedNumberType) {
		this.cachedNumberType = cachedNumberType;
	}

	/**
	 * Returns the type of call.<br/>
	 * Any of {@link android.provider.CallLog.Calls} enumerated types:
	 * <ul>
	 * <li>{@link android.provider.CallLog.Calls#OUTGOING_TYPE OUTGOING_TYPE}</li>
	 * <li>{@link android.provider.CallLog.Calls#INCOMING_TYPE INCOMING_TYPE}</li>
	 * <li>{@link android.provider.CallLog.Calls#MISSED_TYPE MISSED_TYPE}</li>
	 * </ul>
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * Sets the type of call.<br/>
	 * Any of {@link android.provider.CallLog.Calls} enumerated types:
	 * <ul>
	 * <li>{@link android.provider.CallLog.Calls#OUTGOING_TYPE OUTGOING_TYPE}</li>
	 * <li>{@link android.provider.CallLog.Calls#INCOMING_TYPE INCOMING_TYPE}</li>
	 * <li>{@link android.provider.CallLog.Calls#MISSED_TYPE MISSED_TYPE}</li>
	 * </ul>
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * @return the contactLookupKey
	 */
	public String getContactLookupKey() {
		return contactLookupKey;
	}

	/**
	 * @param contactLookupKey the contactLookupKey to set
	 */
	public void setContactLookupKey(String contactLookupKey) {
		this.contactLookupKey = contactLookupKey;
	}

	/**
	 * @return the thumb
	 */
	public Bitmap getThumb() {
		if (thumb != null) return thumb;

		if (this.contactId > 0) {
			ContentResolver cr = CallLogApp.getAppContext().getContentResolver();
			Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, Integer.toString(getContactId()));
			InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
			thumb = BitmapFactory.decodeStream(is);
		}
		
		return thumb;
	}

	/**
	 * @return the contactUri
	 */
	public String getContactUri() {
		return contactUri;
	}

	/**
	 * @param contactUri the contactUri to set
	 */
	public void setContactUri(String contactUri) {
		this.contactUri = contactUri;
	}

	/**
	 * @return the contactId
	 */
	public int getContactId() {
		return contactId;
	}

	/**
	 * @param contactId the contactId to set
	 */
	public void setContactId(int contactId) {
		this.contactId = contactId;
	}
	
	/**
	 * Creates the itemId, by which this log item can be identified
	 * @return String
	 */
	public String getItemId() {
		String itemId = IDPREFIX;
		if (date != null) {
			itemId += DateFormat.format("yyyyMMddkkmmss", date).toString();
		} 
		
		if (number != null) {
			itemId += "#" + number;
		} else {
			itemId += "#0";
		}
		return itemId;
	}
	
}

